package com.example.finallyjob.tool;


import java.util.List;

//分页功能
public class Page<T> {
        //已知数据
        private int pageNum;    //当前页,从请求那边传过来。
        private int pageSize;    //每页显示的数据条数。
        private int totalRecord;    //总的记录条数。查询数据库得到的数据

        private List<T> articles;

    public Page(int pageNum,int pageSize,List<T> articles) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.totalRecord = articles.size();
        int more=totalRecord % pageSize;

        if(more ==0){
            this.totalRecord=totalRecord/pageSize;
        }else{
            this.totalRecord=totalRecord/pageSize+1;
        }

        if(articles.size() > pageSize) {

            if (pageNum > 1 && pageNum < totalRecord) {
                this.articles = articles.subList((pageNum - 1) * pageSize , (pageNum - 1) * pageSize + pageSize );
            } else if (pageNum == totalRecord) {
                System.out.println((pageNum - 1) * pageSize);
                this.articles = articles.subList((pageNum - 1) * pageSize , (pageNum - 1) * pageSize + more );
            } else {
                this.articles = articles.subList(0, pageSize);
            }
        }else {
            this.articles=articles;
        }
    }


    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalRecord() {
        return totalRecord;
    }

    public void setTotalRecord(int totalRecord) {
        this.totalRecord = totalRecord;
    }

    public List<T> getArticles() {
        return articles;
    }

    public void setArticles(List<T> articles) {
        this.articles = articles;
    }
}
